#include <autoconfig.h>
#include "GUI.h"
#include <stdio.h>
extern int uCGUI_loader();
extern void LCD_L0_SetPixelIndex(int x, int y, int PixelIndex);
extern void GUIDEMO_main(void);
void MainTask1(void)
{
	int Cnt = 0;
	int i, YPos;
	int LCDXSize = LCD_GET_XSIZE();
	int LCDYSize = LCD_GET_YSIZE();
	const GUI_BITMAP *pBitmap;
	GUI_Init();
	GUI_SetBkColor(GUI_RED);
	GUI_Clear();
	GUI_Delay(1000);
	GUI_SetBkColor(GUI_BLUE);
	GUI_Clear();
	GUI_ClearRect(0, 0, 100, 100);
	GUI_Delay(1000);
	GUI_SetColor(GUI_WHITE);
	GUI_Delay(1000);
	printf("x %d y %d\r\n", LCDXSize, LCDYSize);
	GUI_DispStringHCenterAt("www.micrium.com", LCDXSize / 2, YPos);

}



#include <LCDConf.h>
void TC_Adj()
{
	int i, k = 0;
	struct point adj[6];
	struct point pt[] = {
		{60,                 60},
		{LCD_XSIZE - 60,                 60},
		{60,                 LCD_YSIZE - 60},
		{LCD_XSIZE - 60 , LCD_YSIZE - 60},
		{LCD_XSIZE / 2, LCD_YSIZE / 2},
		{0, 0},
	};
	char dbgout[128];
	// GUI_SetBkColor
	GUI_SetBkColor(GUI_RED);
	GUI_SetColor(GUI_YELLOW);
	// gl_ui_setbrushcolor(RGB16(255,0,0));
	GUI_FillRect(0, 0 , LCD_XSIZE - 1, LCD_YSIZE - 1);
	GUI_SetColor(GUI_GREEN);
	GUI_FillRect(pt[0].x,  pt[0].y , 4, 4);

	sprintf(dbgout, "%d %d %d ", pt[4].x, pt[4].y, sizeof(pt) / sizeof(struct point));
	GUI_DispStringAt(dbgout, 0, 110);

	k = 1;
	while(k < sizeof(pt) / sizeof(struct point)) {
		// if (TP_IsPress()) {
		// gettouch(&adj[k-1]);
		// g_touch_ms = 1000;
		GUI_Delay(1000);
		// while (gettouch(&adj[k-1]) == 0);
		// gettouch(&adj[k-1]);
		// gl_ui_setpencolor(RGB16(255,0,0));
		for (i = 0; i < sizeof(pt) / sizeof(struct point); i++) {
			GUI_FillRect(pt[i].x,  pt[i].y , 4, 4);
		}
		// gl_ui_setpencolor(RGB16(255,255,255));
		// gl_fill_rect(pt[k].x,  pt[k].y ,4,4);

		sprintf(dbgout, "%d %d %d ", pt[k].x, pt[k].y, k);
		GUI_DispStringAt(dbgout, 0, 110);
		printf("%d %d\r\n", adj[k - 1].x, adj[k - 1].y);
		k++;
		GUI_Delay(300);
		// while (gettouch(&adj[k-1]) == 1);
		// while(TP_IsPress());
		// gettouch(&adj[k-1]);
		// }
	}


	GUI_SetColor(GUI_GREEN);
	for (i = 0; i < sizeof(pt) / sizeof(struct point); i++) {
		GUI_FillRect(pt[i].x,  pt[i].y , 4, 4);
	}


	// tp_adj(pt, adj, &tpadj);
	// sprintf(dbgout, "\n[%f %f %f %f %f %f %f ]\n", (
	// 	tpadj.A1, tpadj.B1, tpadj.C1,
	// 	tpadj.A2, tpadj.B2, tpadj.C2,
	// 	tpadj.K));
	// GUI_DispStringAt(dbgout,0,70);
	sprintf(dbgout, "\n[%d %d\n%d %d\n%d %d\n%d %d\n%d %d\n%d %d]\n",
	        adj[0].x, adj[0].y,
	        adj[1].x, adj[1].y,
	        adj[2].x, adj[2].y,
	        adj[3].x, adj[3].y,
	        adj[4].x, adj[4].y,
	        adj[5].x, adj[5].y);
	GUI_DispStringAt(dbgout, 0, 10);
}

extern void tp_adj(struct point *lcd, struct point *tp, struct adj_tp *retadj);
void myadj()
{
	struct point curpt;
	struct point adj[6];
	struct point pt[] = {
		{60,                 60},
		{LCD_XSIZE - 60,                 60},
		{60,                 LCD_YSIZE - 60},
		{LCD_XSIZE - 60 ,LCD_YSIZE - 60},
		{LCD_XSIZE / 2, LCD_YSIZE / 2},
		{0, 0},
	};
	int index = 0;
	
	GUI_SetBkColor(GUI_RED);
	GUI_SetColor(GUI_BLUE);
	for (int i = 0; i < 6; i++) {
		GUI_FillRect(pt[i].x,  pt[i].y , pt[i].x + 4, pt[i].y + 4);
	}
	GUI_SetColor(GUI_RED);
	GUI_FillRect(pt[index].x,  pt[index].y , pt[index].x + 4, pt[index].y + 4);
	
	
	while(1) {
		if (tp_ispress()) {
			GUI_Delay(40);
			
			
			while (tp_ispress()) {
				gettouch(&adj[index]);
				// printf("pt = %d %d\r\n", adj[index].x, adj[index].y);
				GUI_Delay(100);
			}
			
			GUI_SetColor(GUI_BLUE);
			for (int i = 0; i < 6; i++) {
				GUI_FillRect(pt[i].x,  pt[i].y , pt[i].x + 4, pt[i].y + 4);
			}
			index++;
			GUI_SetColor(GUI_RED);
			GUI_FillRect(pt[index].x,  pt[index].y , pt[index].x + 4, pt[index].y + 4);
			
			if (index >= 5) {
				break;
			}
		}
		GUI_Delay(40);
	}
	struct adj_tp tpadj;
	tp_adj(pt, adj, &tpadj);
	printf("\n[%d %d\n%d %d\n%d %d\n%d %d\n%d %d\n%d %d]\n", 
		adj[0].x,adj[0].y,
		adj[1].x,adj[1].y,
		adj[2].x,adj[2].y,
		adj[3].x,adj[3].y,
		adj[4].x,adj[4].y,
		adj[5].x,adj[5].y);
	
	// printf("\n[%d %d %d %d %d %d %d ]\n", (
	// 	(int)(tpadj.A1*1000), (int)(tpadj.B1*1000), (int)(tpadj.C1*1000), 
	// 	(int)(tpadj.A2*1000), (int)(tpadj.B2*1000), (int)(tpadj.C2*1000), 
	// 	(int)(tpadj.K*1000)));
	
	printf("_A1 = %f\r\n", tpadj.A1);
	printf("_B1 = %f\r\n", tpadj.B1);
	printf("_C1 = %f\r\n", tpadj.C1);
	printf("_A2 = %f\r\n", tpadj.A2);
	printf("_B2 = %f\r\n", tpadj.B2);
	printf("_C2 = %f\r\n", tpadj.C2);
	printf("_K = %f\r\n", tpadj.K);
	tp_setadj(&tpadj);
	
}
int main()
{
	// uCGUI_loader();
	printf("line %d\r\n", __LINE__);
	GUI_Init();
	printf("line %d\r\n", __LINE__);
	GUI_CURSOR_Show();
	printf("line %d\r\n", __LINE__);
	GUI_PID_STATE ts;
	// TPAdjustDlg_Create(NULL);
	// while(1) {
	// 	// GUI_TOUCH_Exec	();
	// 	GUI_TOUCH_GetState(&ts);
	// 	printf("ts %d %d %d\r\n",ts.x, ts.y,ts.Pressed);
	// 	GUI_Exec();
	// 	GUI_Delay(50);
	// }
	myadj();
	MainTask();
	printf("line %d\r\n", __LINE__);
	// GUIDEMO_Touch();
	// GUI_CURSOR_Show();
	while (1) {
		GUI_CURSOR_Show();
		GUI_TOUCH_GetState(&ts);
		printf("ts %d %d %d\r\n", ts.x, ts.y, ts.Pressed);
		GUI_Delay(1000);
	}


	FILE *fp;
	char *pbuf;

	fp = fopen("1.bmp", "r");
	pbuf = (char *)malloc(1024 * 640 * 2);
	fread(pbuf, 1, 1024 * 640 * 2, fp);
	fclose(fp);
	GUI_BMP_Draw(pbuf, 0, 0);
	GUI_Delay(1000);
	while(1) {
		GUIDEMO_main();
	}
	printf("%s() line %d\r\n", __FUNCTION__, __LINE__);
	return 0;
}